<template>
  <div class="login">
    <div class="login-box">
      <div class="login-title">
        <div class="content">
          <span class="welcome">Welcome！</span><br>
          <span class="name">CRM系统</span>
        </div>
      </div>
      <div class="login-form">
        <el-form ref="userRef" :model="user" :rules="rules">
          <el-form-item prop="account" class="wrap mb25">
            <el-input type="text" v-model="user.account" placeholder="请输入用户名">
              <template #prefix>
                <i class="iconfont" :class="'icon-zhanghao1'"></i>
              </template>
            </el-input>
          </el-form-item>
          <el-form-item prop="password" class="wrap mb25">
            <el-input type="password" v-model="user.password" placeholder="请输入密码" @keyup.enter="handleSubmit">
              <template #prefix>
                <i class="iconfont" :class="'icon-mima1'"></i>
              </template>
            </el-input>
            <div class="processing">
              <div class="remember">
                <el-checkbox v-model="rememberMe">记住密码</el-checkbox>
              </div>
              <!--忘记密码-->
              <!--<div class="forgot">-->
              <!--  <el-button type="primary" link @click="getForgot">忘记密码?</el-button>-->
              <!--</div>-->
            </div>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" round class="form-button" @click="handleSubmit">登录</el-button>
          </el-form-item>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script setup>
import { onMounted, ref, reactive } from 'vue'
import { useRouter } from 'vue-router'
// import md5 from 'md5'
// import { handleLogin } from '@/utils/loginUtils'

const router = useRouter()
const userRef = ref(null)
const user = reactive({
  account: 'admin',
  password: '12345678'
})
const rules = reactive({
  account: [{
    required: true,
    message: '请输入账号',
    trigger: 'blur'
  }],
  password: [{
    required: true,
    message: '请输入密码',
    trigger: 'blur'
  }]
})
const rememberMe = ref(false)

const init = () => {
  // 如果已经登录，跳转到数据源页面
  if (localStorage.getItem('loginUser')) {
    router.replace({ name: 'Main' })
  }
}

const handleSubmit = () => {
  userRef.value.validate(async (valid) => {
    if (valid) {
      // const params = {
      //   account: this.user.account,
      //   password: md5(this.user.password)
      // }
      // if (rememberMe) {
      //   localStorage.setItem('savedAccount', user.account)
      //   localStorage.setItem('savedPassword', user.password)
      // } else {
      //   localStorage.removeItem('savedAccount')
      //   localStorage.removeItem('savedPassword')
      // }
      // handleLogin(params).then(res => {
      //   router.push({ name: 'Main' })
      // })
      router.push({ name: 'Main' })
    }
  })
}

// const getForgot = () => {
//   router.push({ name: 'Forgot' })
// }

onMounted(() => {
  const savedAccount = localStorage.getItem('savedAccount')
  const savedPassword = localStorage.getItem('savedPassword')
  if (savedAccount && savedPassword) {
    user.account = savedAccount
    user.password = savedPassword
    rememberMe.value = true
  }
  init()
})

</script>
<style lang="scss">
.login {
  background: url('/images/login/login-back.png') no-repeat;
  background-color: #4788df;
  background-size: cover;
  width: 100%;
  height: 100%;
  position: relative;

  .login-box {
    border-radius: 8px;
    width: 740px;
    height: 400px;
    z-index: 3;
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    display: flex;

    .login-title {
      width: 370px;
      height: 100%;
      background: linear-gradient(143deg, rgba(255, 255, 255, 0.6) 0%, rgba(69, 139, 225, 0.5) 100%);
      border-radius: 10px 0 0 10px;
      display: flex;
      align-items: center;
      justify-content: center;

      .content {
        display: inline-block;
        text-align: left;
        user-select: none;

        .welcome {
          font-size: 36px;
          color: #FFFFFF;
          line-height: 50px;
          font-weight: bold;
        }

        .name {
          font-size: 30px;
          font-weight: 400;
          color: #FFFFFF;
          line-height: 42px;
          padding-top: 12px;
        }
      }
    }

    .login-form {
      flex: 1;
      height: 100%;
      background: #ffffff;
      border-radius: 0 10px 10px 0;
      display: flex;
      align-items: center;

      .el-form {
        width: 86%;
        margin: 0 auto;

        .form-button {
          width: 100%;
          background-color: #3967CD;
          border-color: #3967CD;
          height: 36px;
          line-height: 36px;
        }

        .mb25 {
          margin-bottom: 25px;
        }

        .wrap {
          text-align: left;
          color: #AFB7E1;
        }

        .el-input__wrapper {
          box-shadow: none !important;
          border-bottom: 1px solid #E5E5E5;
          padding: 8px 15px;

          .iconfont {
            font-size: 21px;
            color: #000000;
          }
        }

        svg {
          width: 15px;
          height: 26px;
        }

        .processing {
          width: 100%;
          display: flex;
          padding-top: 5px;

          .remember {}

          .forgot {
            flex: 1;
            text-align: right;

            button {
              text-decoration: underline;
            }
          }
        }
      }
    }
  }
}
</style>
